#!/usr/bin/env bash
# 系统信息
# - 当前发Linux发行版本（名字、LOGO）
# - 当前使用桌面
# - 当前使用Shell

# 设备使用信息
# - 当前CPU使用占比
# - 当前CPU温度值
# - 当前运行内存使用占比
# - 当前磁盘空间使用占比
# - 当前电池电源状态（充电中 电池）
# - 当前电池电量图标
# - 当前电池电量值
# - 当前屏幕背光
# - 当前音量
# - Wi-Fi
# - 有线连接

# 时间信息
# - 当前日期
# - 当前时间
# - 开机时间
# - 开机时长
# - 上一次关机时间

# 安装包信息
## - 总数量
## - 待更新数量

# 获取当前系统linux发行版本名称
function get_dist_name()
{
    if grep -Eqii "Arch Linux" /etc/issue || grep -Eq "Arch Linux" /etc/*-release; then
        DISTRO='Arch Linux'
        # PM='pacman'
    elif grep -Eqii "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
        DISTRO='CentOS'
        # PM='yum'
    elif grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue || grep -Eq "Red Hat Enterprise Linux Server" /etc/*-release; then
        DISTRO='RHEL'
        # PM='yum'
    elif grep -Eqi "Aliyun" /etc/issue || grep -Eq "Aliyun" /etc/*-release; then
        DISTRO='Aliyun'
        # PM='yum'
    elif grep -Eqi "Fedora" /etc/issue || grep -Eq "Fedora" /etc/*-release; then
        DISTRO='Fedora'
        # PM='yum'
    elif grep -Eqi "Debian" /etc/issue || grep -Eq "Debian" /etc/*-release; then
        DISTRO='Debian'
        # PM='apt'
    elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
        DISTRO='Ubuntu'
        # PM='apt'
    elif grep -Eqi "Raspbian" /etc/issue || grep -Eq "Raspbian" /etc/*-release; then
        DISTRO='Raspbian'
        # PM='apt'
    else
        DISTRO='unknow'
    fi

    # 返回值
    echo $DISTRO;
}

# 输入发行版本名字，输出对应图标
function get_dist_icon()
{
    #                     
    #                      
    #  
    distros_icon_icon="";
    # 获取当前系统发行版本
    DISTRO=$(get_dist_name);
    # echo $DISTRO;
    #    ﭾ
    case "$DISTRO" in
        "Arch Linux") distros_icon="" ;;
        "Fedora") distros_icon="" ;;
        "Ubuntu") distros_icon="" ;;
        "Debian") distros_icon="" ;;
        "Aliyun") distros_icon="A" ;;
        "Raspbian") distros_icon="" ;;
        "RHEL") distros_icon="" ;;
        "CentOS") distros_icon="" ;;
        "Deepin") distros_icon="" ;;
        "Manjaro") distros_icon="" ;;
        "Gentoo") distros_icon="" ;;
        "SUSE") distros_icon="" ;;
        "unknow") distros_icon="?" ;;
    esac
    echo $distros_icon" ";
}

# 获取当前使用桌面
function get_wm()
{
  wm=$XDG_CURRENT_DESKTOP
  [ "$wm" ] || wm=$DESKTOP_SESSION

  ## WM/DE
  [ ! "$wm" ] &&
    # loop over all processes and check the binary name
    for i in /proc/*/comm; do
      read -r c <"$i"
      case $c in
      *bar*) ;;
      awesome | xmonad* | qtile | sway | i3 | [bfo]*box | *wm*)
        wm=${c%%-*}
        break
        ;;
      esac
    done

  echo $wm
}

# 获取当前使用shell
function get_shell()
{
    basename $SHELL
}

get_battery() {
    # 图标     
    #  
    # cat /sys/class/power_supply/BAT1/capacity
    if [[ -x "$(which acpi)" ]]; then
        bat_value=$(acpi -b | sed 2d | awk '{print $4}' | grep -Eo "[0-9]+")
        [ ! "$bat_value" ] && bat_value=$(acpi -b | sed 2d | awk '{print $5}' | grep -Eo "[0-9]+")
        # [ ! "$(acpi -b | grep 'Battery 0' | grep Discharging)" ] && charge_icon=""
        # 注：只查一块电池
    else
        bat_value=$(cat /sys/class/power_supply/BAT1/capacity);
    fi

    # if [[ ! "$(acpi -b | grep 'Battery' | grep Discharging)" ]]; then
    if [[ "$(acpi -b | grep 'Battery' | grep Charging)" ]]; then
        bat_status="Charging"; # 有电池充电中
    else
        bat_status="Discharging"; # 
    fi
    
    if [[ $bat_status == "Charging" ]]; then
        bat_icon="";
    else
        if  [ "$bat_value" -ge 95 ]; then  bat_icon="";
        elif [ "$bat_value" -ge 90 ]; then bat_icon="";
        elif [ "$bat_value" -ge 80 ]; then bat_icon="";
        elif [ "$bat_value" -ge 70 ]; then bat_icon="";
        elif [ "$bat_value" -ge 60 ]; then bat_icon="";
        elif [ "$bat_value" -ge 50 ]; then bat_icon="";
        elif [ "$bat_value" -ge 40 ]; then bat_icon="";
        elif [ "$bat_value" -ge 30 ]; then bat_icon="";
        elif [ "$bat_value" -ge 20 ]; then bat_icon="";
        elif [ "$bat_value" -ge 10 ]; then bat_icon="";
        else bat_icon=""; fi
        # bat_text=$bat_text%
        # bat_icon=$charge_icon$bat_icon
    fi

    if [[ $1 == "--icon" ]]; then
        echo $bat_icon
    elif [[ $1 == "--status" ]]; then 
        echo $bat_status
    elif [[ $1 == "--value" ]]; then
        echo $bat_value
    else
        echo $bat_icon" "$bat_value"%"
    fi

    # text=" $bat_icon $bat_text"
    # color=$bat_color
    # printf "%s" "$text" 
}

# 获取当前系统背光亮度设置百分比
function get_brightness_pcent()
{
    # 图标：                               
    #    ﯷ ﱫ ﱪ ﳺ  盛 ﱳ ﲏ ﮻  盛 
    #         
    # 显卡设备 radeon_bl0  intel_backlight
    if [[ -x "$(which brightnessctl)" ]]; then
        #statements
        # echo "[DEDUG] 已安装bluetoothctl";
        # notify-send "[DEDUG] 已安装bluetoothctl";
        brightness_percent=$(brightnessctl i -m | tr , " " | awk '{print $4}' | cut -c 1-2)
        # echo $bri;
    else
        # notify-send "[DEDUG]没有安装bluetoothctl";
        # echo "[DEDUG]没有安装bluetoothctl";
        # 使用其他方式
        now_brightness_value=$(cat /sys/class/backlight/*/brightness);
        max_brightness_value=$(cat /sys/class/backlight/*/max_brightness);
        brightness_rate=$((now_brightness_value * 100 / max_brightness_value))
        brightness_percent=$(echo $brightness_rate | awk '{printf "%02d", $1}')
        # [Linux——屏幕亮度调节 - 走看看](http://t.zoukankan.com/droidxin-p-3695174.html)
        # [linux 命令获取屏幕亮度值，设置屏幕亮度_zyong_xu的博客-CSDN博客](https://blog.csdn.net/azure_2010/article/details/100978016)
        # [Linux下调节屏幕亮度(Intel核显)_Linux教程_Linux公社-Linux系统门户网站](https://www.linuxidc.com/Linux/2012-03/57098.htm)
        # [archlinux设置屏幕亮度_java_xiaoer的博客-CSDN博客](https://blog.csdn.net/java_xiaoer/article/details/51457620?locationNum=6&fps=1)
    fi

    # -eq 等于 -lt 小于 -ne 不等于 -gt 大于 -le 小于等于 -ge 大于等于
    # if   [ "$brightness_percent" -eq 0  ]; then brightness_icon="";
    if   [ "$brightness_percent" -eq 0  ]; then brightness_icon="";
    elif [ "$brightness_percent" -lt 10 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 20 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 30 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 40 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 50 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 60 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 70 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 80 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 80 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 90 ]; then brightness_icon="";
    elif [ "$brightness_percent" -le 99 ]; then brightness_icon="";
    elif [ "$brightness_percent" -eq 100 ]; then brightness_icon="盛";
    else brightness_icon=""; 
    fi
    
    if [[ $1 == "--value" ]]; then
        echo $brightness_percent;
    elif [[ $1 == "--icon" ]]; then
        echo $brightness_icon;
    else
        brightness_text=$brightness_percent%
        text=" $brightness_icon $brightness_text"
        echo $text;
    fi
}

# 获取当前系统蓝牙信息
function get_bluetooth_info()
{

    # not connected
    if [ $(bluetoothctl show | grep "Powered: yes" | wc -c) -eq 0 ]; then
        bluetooth_battery="Currently Off"
    else
        # on
        if [ $(echo info | bluetoothctl | grep 'Device' | wc -c) -eq 0 ]; then
            bluetooth_battery="Not Connected"
        else
            BATTERY=`upower -i /org/freedesktop/UPower/devices/headset_dev_33_33_55_33_90_D0 | grep percentage | cut -b 26-28`
            bluetooth_battery="Battery Level: $BATTERY"
        fi
    fi

    bluetoothctl_status="Off"
    if [[ -x "$(which bluetoothctl)" ]]; then
        #statements
        # echo "[DEDUG] 已安装bluetoothctl";
        # notify-send "[DEDUG] 已安装bluetoothctl";
        # not connected
        if [ $(bluetoothctl show | grep "Powered: yes" | wc -c) -eq 0 ]; then
            bluetoothctl_status="Off"
        else
            bluetoothctl_status="On"
        fi
    else
        notify-send "[DEDUG]没有安装bluetoothctl";
        # echo "[DEDUG]没有安装bluetoothctl";
        bluetoothctl_status="Off"
    fi

    if [[ $bluetoothctl_status == "On" ]]; then
        # on
        if [ $(echo info | bluetoothctl | grep 'Device' | wc -c) -eq 0 ]; then
            bluetoothctl_icon=""
        else
            # get device alias
            DEVICE=`echo info | bluetoothctl | grep 'Alias:' | awk -F:  '{ print $2 }'`
            bluetoothctl_device="$DEVICE"
            bluetoothctl_icon=""
        fi
    else
        bluetoothctl_icon=""
    fi

    if [[ $1 == "--status" ]]; then
        echo $bluetoothctl_status
    elif [[ $1 == "--icon" ]]; then
        echo $bluetoothctl_icon
    elif [[ $1 == "--battery" ]]; then
        echo $bluetooth_battery
    else
        echo $bluetoothctl_icon" "$bluetoothctl_device
    fi
}

# 开关蓝牙
function toggle_bluetooth()
{
    STATUS="$(bluetoothctl show | grep Powered | awk '{print $2}')"
    if [ $STATUS == "yes" ]; then
        bluetoothctl power off
        notify-send --icon=bluetooth-offline --urgency=normal "Bluetooth" "Bluetooth has been turned off."
    else
        bluetoothctl power on
        notify-send --icon=volume-level-high --urgency=normal "Bluetooth" "Bluetooth has been turned on."
    fi
}

# 获得无线wifi设备信息
get_wireless_info()
{
    wireless_status=$(cat /sys/class/net/wl*/operstate 2>/dev/null)
    # wireless_status=$(nmcli g | grep -oE "disconnected")
    wireless_con_status="unknown"
    # nmcli networking connectivity
    # 网络连接状态共有五种：
    # - full
    # - limited （连网，但无法上网）
    # - portal （连网，但需要认证登录后才能上网）
    # - none （没连网）
    # - unknown
    # case "$wireless_status" in
    #     up) wireless_icon="󰤨" ;
    #     down) wireless_icon= "󰤭" ;;
    # esac
    #    # 直 睊  ﲁ 
    if [[ $wireless_status == "down" ]]; then
        wireless_icon="睊"
    else
        wireless_icon="直"
        wireless_con_status=$(nmcli n c)
        case "$wireless_con_status" in
            full) wireless_icon="" ;;
            limited) wireless_icon="直" ;;
            portal) wireless_icon="直" ;;
            none) wireless_icon="直" ;;
            unknown) wireless_icon="直" ;;
        esac
    fi
    # status=$(nmcli g | grep -oE "disconnected")
    wireless_device=$(ls /sys/class/net | grep wl)
    wireless_essid=$(nmcli c | grep $wireless_device | awk '{print ($1)}')

    if [[ $1 == "--status" ]]; then
        echo $wireless_status
    elif [[ $1 == "--con" ]]; then
        echo $wireless_con_status
    elif [[ $1 == "--icon" ]]; then
        echo $wireless_icon
    elif [[ $1 == "--essid" ]]; then
        echo $wireless_essid
    else
        echo $wireless_icon" "$wireless_essid
    fi
}

# 开关wifi设备
toggle_wireless()
{
    wireless_status=$(cat /sys/class/net/wl*/operstate 2>/dev/null)
    wireless_device=$(ls /sys/class/net | grep wl)
    if [[ $wireless_status == 'up' ]]; then
        # 关闭wifi
        # echo $wireless_device
        nmcli n off
        notify-send "Wireless has been turned off."
    else
        # 开启wifi
        # echo $wireless_device
        nmcli n on
        notify-send "Wireless has been turned on."
    fi
    # echo $wireless_device
}

# 获取声音设备信息
function get_volume()
{
    volume_status=$(awk '/%/ {gsub(/[\[\]]/,""); print $6}' <(amixer sget Master) | awk 'FNR == 1')
    if [[ $volume_status == "on" ]]; then
        # search for the lines containing 'Right:' or 'Mono:', when more than 1 field exists
        # we strip the trailing '%' and round it up with printf "%0.0f" just in case
        volume_value=$(amixer sget Master | awk -F'[][]' '/Right:|Mono:/ && NF > 1 {sub(/%/, ""); printf "%0.0f\n", $2}')
        # -eq 等于 -lt 小于 -ne 不等于 -gt 大于 -le 小于等于 -ge 大于等于
        if [[ "${volume_value}" -ge 80 ]]; then
            volume_icon=""
        elif [[ "${volume_value}" -ge 40 ]]; then
            volume_icon="奔"
        elif [[ "${volume_value}" -ge 10 ]]; then
            volume_icon=""
        else
            volume_icon=""
        fi
    else
        vol_status="off"
        volume_value=$(amixer sget Master | awk -F'[][]' '/Right:|Mono:/ && NF > 1 {sub(/%/, ""); printf "%0.0f\n", $2}')
        # -eq 等于 -lt 小于 -ne 不等于 -gt 大于 -le 小于等于 -ge 大于等于
        if [[ "${volume_value}" -ge 10 ]]; then
            volume_icon="󰖁"
        else
            volume_icon=""
        fi
    fi

    if [[ $1 == "--status" ]]; then
        echo $volume_status
    elif [[ $1 == "--value" ]]; then
        echo $volume_value
    elif [[ $1 == "--icon" ]]; then
        echo $volume_icon
    else
        echo $volume_icon" "$volume_value"%"
    fi
}

# 开关声音 
function toggle_volume()
{
    volume_status=$(awk '/%/ {gsub(/[\[\]]/,""); print $6}' <(amixer sget Master) | awk 'FNR == 1')
    if [ $volume_status == "on" ]; then
        amixer set Master toggle
        notify-send --icon=volume-level-muted --urgency=normal "Volume " "Volume has been muted!"
    else
        amixer set Master toggle
        notify-send --icon=volume-level-high --urgency=normal "Volume" "Volume has been unmuted!"
    fi
}

# 更新间隔时长
function get_uptime() 
{
        time="$(uptime -p )"
        time="${time/up }"
        time="${time/ day,/d}"
        time="${time/ days,/d}"
        time="${time/ hours,/h}"
        time="${time/ minutes/m}"
        echo ${time:-'less than a minute'}
}

# 最近更新时间点
function get_uptime_at()
{
    at_time="$(uptime -s )"
    echo $at_time
}

# cpu信息
function get_cpuavg()
{
    get_cpuavg=$(grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}')
    echo $get_cpuavg;
}

# "$@"
if [[ $1 == "--distros" ]]; then
    # 获取当前系统发行版本名字
    get_dist_name
elif [[ $1 == "--osicon" ]]; then
    # 获取当前系统发行版logo图标
    get_dist_icon $(get_dist_name)
elif [[ $1 == "--wm" ]]; then
    # 获取当前系统桌面wm
    get_wm
elif [[ $1 == "--shell" ]]; then
    # 获取当前系统Shell
    get_shell
elif [[ $1 == "--brightness" ]]; then
    # 获取当前系统背光亮度设置百分比
    #  
    get_brightness_pcent
elif [[ $1 == "--brightness_value" ]]; then
    # 获取当前系统背光亮度值，不带%
    get_brightness_pcent "--value"
elif [[ $1 == "--brightness_icon" ]]; then
    # 获取当前系统背光亮度设置百分比值，不带%
    get_brightness_pcent "--icon"
elif [[ $1 == "--uptime" ]]; then
    get_uptime
elif [[ $1 == "--uptimeat" ]]; then
    get_uptime_at
elif [[ $1 == "--cpuavg" ]]; then
    get_cpuavg
elif [[ $1 == "--battery" ]]; then
    get_battery
elif [[ $1 == "--battery_status" ]]; then
    get_battery '--status'
elif [[ $1 == "--battery_icon" ]]; then
    get_battery '--icon'
elif [[ $1 == "--battery_value" ]]; then
    get_battery '--value'
elif [[ $1 == "--bluetooth_status" ]]; then
    get_bluetooth_info '--status'
elif [[ $1 == "--bluetooth_icon" ]]; then
    get_bluetooth_info '--icon'
elif [[ $1 == "--bluetooth" ]]; then
    get_bluetooth_info
elif [[ $1 == "--bluetooth_battery" ]]; then
    get_bluetooth_info '--battery'
elif [[ $1 == "--bluetooth_toggle" ]]; then
    toggle_bluetooth
elif [[ $1 == "--wireless" ]]; then
    get_wireless_info
elif [[ $1 == "--wireless_battery" ]]; then
    get_wireless_info '--battery'
elif [[ $1 == "--wireless_status" ]]; then
    get_wireless_info '--status'
elif [[ $1 == "--wireless_icon" ]]; then
    get_wireless_info '--icon'
elif [[ $1 == "--wireless_toggle" ]]; then
    toggle_wireless
elif [[ $1 == "--wireless_essid" ]]; then
    get_wireless_info '--essid'
elif [[ $1 == "--volume" ]]; then
    get_volume
elif [[ $1 == "--volume_status" ]]; then
    get_volume '--status'
elif [[ $1 == "--volume_value" ]]; then
    get_volume '--value'
elif [[ $1 == "--volume_icon" ]]; then
    get_volume '--icon'
elif [[ $1 == "--volume_toggle" ]]; then
    toggle_volume
else
    echo "请添加输入参数，例如: --osicon"
fi


# 
# 奄 奔 墳 婢  ﱛ ﱜ ﱝ 
# 直 睊  ﲁ 